package com.manage.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.dto.VwUserActivityDetailsDTO;
import com.manage.entity.VwUserActivityDetails;
import com.manage.result.PageResult;
import com.manage.server.mapper.VwUserActivityDetailsMapper;
import com.manage.server.service.VwUserActivityDetailsService;
import com.manage.vo.VwUserActivityDetailsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @description 针对视图【vw_user_activity_details(用户活动详情视图)】的数据库操作Service实现
 */
@Service
public class VwUserActivityDetailsServiceImpl extends ServiceImpl<VwUserActivityDetailsMapper, VwUserActivityDetails>
        implements VwUserActivityDetailsService {


    @Override
    public PageResult<VwUserActivityDetailsVO> findVwUserActivityDetailsByPage(VwUserActivityDetailsDTO dto) {
        // 1. 处理分页参数（严格保持默认值设置）
        int pageNum = dto.getPageNum() != null && dto.getPageNum() > 0 ? dto.getPageNum() : 1;
        int pageSize = dto.getPageSize() != null && dto.getPageSize() > 0 ? dto.getPageSize() : 10;

        // 2. 创建分页对象（严格保持参数顺序）
        Page<VwUserActivityDetails> page = new Page<>(pageNum, pageSize);

        // 3. 构建动态查询条件（严格按视图字段构建）
        QueryWrapper<VwUserActivityDetails> queryWrapper = new QueryWrapper<>();

        // 精确匹配条件
        if (dto.getUserActivityId() != null) {
            queryWrapper.eq("user_activity_id", dto.getUserActivityId());
        }
        if (dto.getUserId() != null) {
            queryWrapper.eq("user_id", dto.getUserId());
        }
        if (dto.getActivityId() != null) {
            queryWrapper.eq("activity_id", dto.getActivityId());
        }

        // 模糊查询条件
        if (StringUtils.hasText(dto.getUserName())) {
            queryWrapper.like("user_name", dto.getUserName().trim());
        }
        if (StringUtils.hasText(dto.getPhone())) {
            queryWrapper.like("phone", dto.getPhone().trim());
        }
        if (StringUtils.hasText(dto.getActivityName())) {
            queryWrapper.like("activity_name", dto.getActivityName().trim());
        }
        if (StringUtils.hasText(dto.getLocationAddress())) {
            queryWrapper.like("location_address", dto.getLocationAddress().trim());
        }

        // 状态条件
        if (StringUtils.hasText(dto.getActivityStatus())) {
            queryWrapper.eq("activity_status", dto.getActivityStatus());
        }
        if (StringUtils.hasText(dto.getAllowSelfEnroll())) {
            queryWrapper.eq("allow_self_enroll", dto.getAllowSelfEnroll());
        }

        // 时间范围查询
        if (StringUtils.hasText(dto.getStartTime())) {
            queryWrapper.ge("start_time", dto.getStartTime());
        }
        if (StringUtils.hasText(dto.getEndTime())) {
            queryWrapper.le("end_time", dto.getEndTime());
        }

        // 4. 执行分页查询（视图查询与实体查询方式一致）
        Page<VwUserActivityDetails> resultPage = this.page(page, queryWrapper);

        // 5. 转换Entity到VO（严格保持字段映射）
        Page<VwUserActivityDetailsVO> voPage = (Page<VwUserActivityDetailsVO>) resultPage.convert(entity -> {
            VwUserActivityDetailsVO vo = new VwUserActivityDetailsVO();
            BeanUtils.copyProperties(entity, vo);
            // 处理视图字段与VO字段的特殊映射
            // vo.setCustomField(entity.getXXX());
            return vo;
        });

        // 6. 返回分页结果（严格保持统一格式）
        return PageResult.success(voPage);
    }
}